"""
Case Type   : spq多机并行
Case Name   : 配置spq相关参数
Create At   : 2024/04/08
Owner       : @songjing20
Description :
    1.配置postgresql.conf
    2.配置pg_hba.conf
Expect      :
    1.成功
    2.成功
History     :
    Modified by songjing20 2024/6/12:修改所有节点多机并行为仅主节点和备1节点多机并行
"""

import os
import unittest

from yat.test import macro
from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 > Primary_SH.get_node_num(), '非1+2以上环境不执行')
class Pubsub(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node(node='PrimaryDbUser')
        self.std1_node = Node(node='Standby1DbUser')
        self.std2_node = Node(node='Standby2DbUser')
        self.commsh_std1 = CommonSH('Standby1DbUser')
        self.constant = Constant()
        self.port = self.pri_node.db_port
        self.pgxc_node = {Primary_SH: 'dn_6001', self.commsh_std1: 'dn_6002'}
        self.cluster_map = f'dn_6001|{self.pri_node.db_host}|{self.port},' \
                           f'dn_6002|{self.std1_node.db_host}|{self.port}'
        self.log.info('----- 集群状态 -----')
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, f'集群状态异常')

    def test_pubsub(self):
        text = '-----step1:配置postgresql.conf;expect:成功-----'
        guc_res = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "shared_preload_libraries = 'security_plugin,spqplugin'")
        self.assertTrue(guc_res, f'执行失败:{text}')
        for node, name in self.pgxc_node.items():
            guc_res = node.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f"pgxc_node_name = '{name}'", single=True)
            self.assertTrue(guc_res, f'设置pgxc_node_name失败:{text}')

        cmd = f"echo \\\"spqplugin.cluster_map='{self.cluster_map}'\\\"" \
              f" >> $PGDATA/postgresql.conf"
        res = Primary_SH.exec_gs_ssh(cmd)
        self.log.info(res)
        self.assertEqual(res.count('[SUCCESS]'), 3, f'执行失败:{text}')
        restart_res = Primary_SH.restart_db_cluster()
        self.assertTrue(restart_res, f'重启集群失败:{text}')

        text = '-----step2:配置pg_hba.conf;expect:成功-----'
        self.log.info(text)
        guc_res = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  all  {self.pri_node.db_user}  '
            f'{self.pri_node.db_host}/32 trust')
        self.log.info(guc_res)
        self.assertTrue(guc_res, f'配置失败:{text}')

        guc_res = Primary_SH.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG, '',
            'all', False, False, '',
            f'host  all  {self.pri_node.db_user}  '
            f'{self.std1_node.db_host}/32 trust')
        self.log.info(guc_res)
        self.assertTrue(guc_res, f'配置失败:{text}')

    def tearDown(self):
        self.log.info('-----不恢复环境，执行sql用例-----')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
